# Handles rendering everything to the screen

import libtcodpy as libtcod
import Messages

class Render:
    @staticmethod
    def render_all(gamestate, chat=""):    
        if gamestate.fov_recompute:
            gamestate.fov_recompute = False
            libtcod.map_compute_fov(gamestate.fov_map, gamestate.player.x, gamestate.player.y, gamestate.TORCH_RADIUS,
                                gamestate.FOV_LIGHT_WALLS, gamestate.FOV_ALGO)
            for y in range(gamestate.map.MAP_HEIGHT):
                for x in range(gamestate.map.MAP_WIDTH):
                    libtcod.console_set_back(gamestate.con, x, y, gamestate.map.color_empty_map, libtcod.BKGND_SET)
                    visible = libtcod.map_is_in_fov(gamestate.fov_map, x, y)
                    wall = gamestate.map.map[x][y].block_sight
                    if not visible:
                        if gamestate.map.map[x][y].explored:
                            if wall:
                                libtcod.console_set_back(gamestate.con, x, y, gamestate.map.color_dark_wall, libtcod.BKGND_SET)
                            else:
                                libtcod.console_set_back(gamestate.con, x, y, gamestate.map.color_dark_ground, libtcod.BKGND_SET)
                    else:
                        if wall:
                            libtcod.console_set_back(gamestate.con, x, y, gamestate.map.color_light_wall, libtcod.BKGND_SET)
                        else:
                            libtcod.console_set_back(gamestate.con, x, y, gamestate.map.color_light_ground, libtcod.BKGND_SET)
                        gamestate.map.map[x][y].explored = True
        
        for game_object in gamestate.game_objects:
            if game_object != gamestate.player:
                game_object.draw()
            gamestate.player.draw()
            
        libtcod.console_blit(gamestate.con, 0, 0, gamestate.SCREEN_WIDTH, gamestate.SCREEN_HEIGHT, 0, 0, 0)
        libtcod.console_set_background_color(gamestate.panel.panel, libtcod.black)
        libtcod.console_clear(gamestate.panel.panel)
        gamestate.panel.render_hp()
        gamestate.panel.render_msgs()
        gamestate.panel.render_chat(chat)
        libtcod.console_blit(gamestate.panel.panel, 0, 0, gamestate.map.MAP_WIDTH, gamestate.panel.PANEL_HEIGHT, 0, 0,
                             gamestate.SCREEN_HEIGHT - gamestate.panel.PANEL_HEIGHT)
        libtcod.console_flush()
        
class InfoPanel:
    def __init__(self, gamestate):
        self.gamestate = gamestate
        self.BAR_WIDTH = 20
        self.MSG_X = self.BAR_WIDTH + 2
        self.MSG_WIDTH = gamestate.SCREEN_WIDTH - self.BAR_WIDTH - 2
        self.PANEL_HEIGHT = 6
        self.PANEL_Y = 1
        self.panel = libtcod.console_new(gamestate.SCREEN_WIDTH, self.PANEL_HEIGHT)
        
    def render_hp(self):
        bar_width = int(float(self.gamestate.player.fighter.hp) / self.gamestate.player.fighter.max_hp * self.BAR_WIDTH)
        libtcod.console_set_background_color(self.panel, libtcod.black)
        libtcod.console_rect(self.panel, 1, self.PANEL_Y, self.BAR_WIDTH, 1, False)
        quarter_left = self.gamestate.player.fighter.max_hp * 0.25
        if self.gamestate.player.fighter.hp > quarter_left:
            libtcod.console_set_background_color(self.panel, libtcod.dark_green)
        else:
            libtcod.console_set_background_color(self.panel, libtcod.dark_red)
        if bar_width > 0:
            libtcod.console_rect(self.panel, 1, self.PANEL_Y, bar_width, 1, False)
        libtcod.console_set_foreground_color(self.panel, libtcod.white)
        libtcod.console_print_center(self.panel, 1 + self.BAR_WIDTH / 2, self.PANEL_Y, libtcod.BKGND_NONE,
                                     'HP: ' + str(self.gamestate.player.fighter.hp) + '/' + str(self.gamestate.player.fighter.max_hp))
        
    def render_chat(self, chat=""):
        libtcod.console_set_foreground_color(self.panel, libtcod.white)
        libtcod.console_print_left(self.panel, 1, 4, libtcod.BKGND_NONE, chat)
        
    def render_msgs(self):
        y=1
        for msg in Messages.messages:
            libtcod.console_set_foreground_color(self.panel, msg[1])
            libtcod.console_print_left(self.panel, 25, y, libtcod.BKGND_NONE, msg[0])
            y += 1
        
        
                    